package cn.iocoder.yudao.module.system.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 教师看板异步配置
 * 配置异步任务执行器，优化性能
 *
 * @author 芋道源码
 */
@Configuration
@EnableAsync
@Slf4j
public class TeacherDashboardAsyncConfig {

    /**
     * 教师看板专用线程池
     */
    @Bean("dashboardExecutor")
    public Executor dashboardExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        
        // 核心线程数：CPU核心数
        int corePoolSize = Runtime.getRuntime().availableProcessors();
        executor.setCorePoolSize(corePoolSize);
        
        // 最大线程数：核心线程数的2倍
        executor.setMaxPoolSize(corePoolSize * 2);
        
        // 队列容量：100个任务
        executor.setQueueCapacity(100);
        
        // 线程名称前缀
        executor.setThreadNamePrefix("dashboard-async-");
        
        // 线程空闲时间：60秒
        executor.setKeepAliveSeconds(60);
        
        // 拒绝策略：调用者运行策略（在调用者线程中运行被拒绝的任务）
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        // 等待所有任务完成后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        
        // 等待时间：30秒
        executor.setAwaitTerminationSeconds(30);
        
        // 初始化线程池
        executor.initialize();
        
        log.info("教师看板异步线程池初始化完成，核心线程数: {}, 最大线程数: {}, 队列容量: {}", 
                corePoolSize, corePoolSize * 2, 100);
        
        return executor;
    }

    /**
     * 缓存预热专用线程池
     */
    @Bean("cachePreloadExecutor")
    public Executor cachePreloadExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        
        // 缓存预热使用较少的线程，避免对数据库造成过大压力
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(4);
        executor.setQueueCapacity(50);
        executor.setThreadNamePrefix("cache-preload-");
        executor.setKeepAliveSeconds(120);
        
        // 缓存预热任务被拒绝时直接丢弃（非关键任务）
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        
        executor.initialize();
        
        log.info("缓存预热线程池初始化完成，核心线程数: 2, 最大线程数: 4, 队列容量: 50");
        
        return executor;
    }

    /**
     * 数据统计专用线程池
     */
    @Bean("statisticsExecutor")
    public Executor statisticsExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        
        // 统计任务通常比较耗时，使用适中的线程数
        executor.setCorePoolSize(3);
        executor.setMaxPoolSize(6);
        executor.setQueueCapacity(200);
        executor.setThreadNamePrefix("statistics-");
        executor.setKeepAliveSeconds(300);
        
        // 统计任务重要性较高，使用调用者运行策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(120);
        
        executor.initialize();
        
        log.info("数据统计线程池初始化完成，核心线程数: 3, 最大线程数: 6, 队列容量: 200");
        
        return executor;
    }
}